package haut.util;

import haut.system.member.events.SportingEvents;
import haut.system.member.person.Player;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

/**
 * <code>UtilOfEvents</code>是一个项目工具类。
 * @author MingJie Li
 */
public class UtilOfSportsEvents {
    /**
     * 静态方法。
     * 对SportsEvents下的Player类型的Vector容器进行排序。
     * @param events 待排序运动员得分的项目
     * @return {Vector<Player>} 返回有序的运动员得分情况
     * */
    public static Vector<Player> ScoreSort(SportingEvents events) {
        class ScoreCompare implements Comparator {

            @Override
            public int compare(Object o1, Object o2) {
                Player player1 = (Player) o1;
                Player player2 = (Player) o2;

                // player项目的集合
                Vector<Player.Events> myEvents1 = player1.getMyEvents();
                Vector<Player.Events> myEvents2 = player2.getMyEvents();

                // player该项目的得分
                double score1 = 0.0;
                double score2 = 0.0;

                // player1
                // 找到对应项目后，记录得分
                for(Player.Events playerEvents : myEvents1)
                    if (playerEvents.getEvents().getId() == events.getId()) {
                        score1 = playerEvents.getScore();
                        break;
                    }

                // player2
                for(Player.Events playerEvents : myEvents2) {
                    // 找到对应项目后，记录得分
                    if(playerEvents.getEvents().getId() == events.getId()) {
                        score2 = playerEvents.getScore();
                        break;
                    }
                }

                if(score1 > score2) {
                    return 1;
                } else if (score1 == score2) {
                    return 0;
                } else {
                    return -1;
                }
            }
        }
        Vector<Player> temp = events.getPlayer();
        temp.sort(new ScoreCompare());
        return temp;
    }
}
